package com.cskaoyan.market.controller;

import com.cskaoyan.market.bo.AdminConfigExpressBo;
import com.cskaoyan.market.bo.AdminConfigOrderBo;
import com.cskaoyan.market.service.AdminConfigService;
import com.cskaoyan.market.service.impl.AdminConfigServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.AdminConfigExpressVo;
import com.cskaoyan.market.vo.AdminConfigOrderVo;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@WebServlet("/admin/config/*")
public class AdminConfigController extends HttpServlet {

    AdminConfigService adminConfigService = new AdminConfigServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config", "");

        if("express".equals(op)) {
            getExpress(req, resp);
        } else if ("order".equals(op)) {
            getOrder(req, resp);
        }


    }
    private void getExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AdminConfigExpressVo adminConfigExpressVo = adminConfigService.getExpress();

        Object o = ResponseUtil.ok(adminConfigExpressVo);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s);
    }

    private void getOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        AdminConfigOrderVo adminConfigOrderVo = adminConfigService.getOrder();

        Object o = ResponseUtil.ok(adminConfigOrderVo);
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s);
    }




    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/config/", "");
        if("express".equals(op)) {
            postExpress(req, resp);
        } else if ("order".equals(op)) {
            postOrder(req, resp);
        }


    }

    private void postOrder(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ServletInputStream inputStream = req.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
        String str = bufferedReader.readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        AdminConfigOrderBo adminConfigOrderBo = objectMapper.readValue(str, AdminConfigOrderBo.class);
        adminConfigService.postOrder(adminConfigOrderBo);

        Object o = ResponseUtil.ok();
        String s = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s);
    }

    private void postExpress(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        ServletInputStream inputStream = req.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));

        String s = bufferedReader.readLine();
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        AdminConfigExpressBo adminConfigExpressBo = objectMapper.readValue(s, AdminConfigExpressBo.class);

        adminConfigService.postExpress(adminConfigExpressBo);

        Object o = ResponseUtil.ok();
        String s1 = JacksonUtil.getObjectMapper().writeValueAsString(o);
        resp.getWriter().write(s1);

    }
}
